﻿
/*
date	   :2010-10-5
author	   :by wangchao cwangchao@gmail.com
*/
function CRequest() 
{ 
    this.XmlHttp = this.GetXMLHttpRequest();
    this.request_fun=false;
} 
CRequest.prototype.GetXMLHttpRequest = function()
{ 
    var http_request;
   	if (window.XMLHttpRequest) { // Mozilla, Safari,...
   		http_request = new XMLHttpRequest();
   		 if (http_request.overrideMimeType) {
        		http_request.overrideMimeType('text/xml');
   		}
	} else if (window.ActiveXObject) { // IE
	  	  try {
  		 	     http_request = new ActiveXObject("Msxml2.XMLHTTP");
  	   } catch (e) {
    		    try {
   			        http_request = new ActiveXObject("Microsoft.XMLHTTP");
    		    } catch (e) {}
	 	   }
	}
	if (!http_request) 
        {
          		  alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
        return http_request;
} 
CRequest.prototype.Request = function(url,method,data,request_fun) 
{ 
     m_request_fun=request_fun;
     if( this.XmlHttp ) 
     { 
          if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 ) 
          { 
           var oThis = this;
           this.XmlHttp.onreadystatechange = function() 
           {
               oThis.ReadyStateChange(); 
           };  
           this.XmlHttp.open(method, url, true);
	       this.XmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
           this.XmlHttp.setRequestHeader("If-Modified-Since","0");
           this.XmlHttp.send(data); 
          } 
     } 
} 
CRequest.prototype.AbortCallBack = function() 
{ 
    if( this.XmlHttp ) 
    this.XmlHttp.abort(); 
} 
CRequest.prototype.ReadyStateChange = function() 
{ 
     if (this.XmlHttp.readyState == 1) 
     { 
         this.OnLoading(); 
     } 
     else if (this.XmlHttp.readyState == 2) 
     { 
          this.OnLoaded(); 
     } 
     else if (this.XmlHttp.readyState == 3) 
     {    
          this.OnInteractive(); 
     }  
     else if (this.XmlHttp.readyState == 4) 
     { 
          if (this.XmlHttp.status == 0) 
          this.OnAbort(); 
          else if (this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK") 
          this.OnComplete();   
          else 
          this.OnError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText); 
     } 
} 
CRequest.prototype.OnLoading = function() 
{ 
    // Loading 
} 
CRequest.prototype.OnLoaded = function() 
{ 
    // Loaded 
} 
CRequest.prototype.OnInteractive = function() 
{ 
    // Interactive 
} 
CRequest.prototype.OnComplete = function() 
{ 
    // Complete 
    if(m_request_fun&&typeof m_request_fun!='undefined') 
    {
        m_request_fun(this.XmlHttp);
    }
} 
CRequest.prototype.OnAbort = function() 
{ 
    // Abort 
} 
CRequest.prototype.OnError = function(status, statusText) 
{ 
     // Error 
} 


